import sys
from pathlib import Path
import pytest
import asyncio
from redis.asyncio import Redis

# 获取项目根目录
root_dir = Path(__file__).parent.parent

# 将项目根目录添加到Python路径
sys.path.insert(0, str(root_dir))

def pytest_configure(config):
    """配置 pytest-asyncio"""
    config.addinivalue_line(
        "markers",
        "asyncio: mark test as requiring asyncio"
    )

@pytest.fixture(scope="session")
def event_loop_policy():
    """设置事件循环策略"""
    policy = asyncio.WindowsSelectorEventLoopPolicy()
    asyncio.set_event_loop_policy(policy)
    return policy

@pytest.fixture(scope="function")
async def redis_client():
    """Redis客户端fixture"""
    client = None
    try:
        client = Redis(host='localhost', port=6379, db=0)
        await client.flushdb()  # 清理数据
        yield client
    finally:
        if client:
            await client.aclose()
            await asyncio.sleep(0.1)  # 给予足够的时间来清理连接